﻿using System.Collections;
using blackenter.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackenter.Tests.FakeDbContexts {
    public class FakeSiteDbContext : ISiteDbContext {

        #region Fields

        private static readonly List<SiteData> sites = new List<SiteData>();
        private static readonly Dictionary<int, SortedList<int, string>> siteLanguages = new Dictionary<int, SortedList<int, string>>();
        private static readonly Dictionary<int, string> siteDefaultLanguages = new Dictionary<int, string>();

        #endregion

        #region Properties

        public List<SiteData> Sites {
            get {
                return sites;
            }
        }

        public Dictionary<int, SortedList<int, string>> SiteLanguages {
            get {
                return siteLanguages;
            }
        }

        public Dictionary<int, string> SiteDefaultLanguages {
            get {
                return siteDefaultLanguages;
            }
        }

        #endregion

        #region Methods

        internal static void Reset() {
            sites.Clear();
            siteLanguages.Clear();
            siteDefaultLanguages.Clear();
        }

        public int SaveNewSite(string siteName) {
            var data = new SiteData
            {
                Id = this.Sites.Count + 1,
                Name = siteName
            };
            this.Sites.Add(data);
            return data.Id;
        }

        public SiteData GetSiteBySiteId(int siteId) {
            return this.Sites.FirstOrDefault(s => s.Id.Equals(siteId));
        }

        public bool SaveSite(int siteId, string siteName) {
            var data = this.Sites.FirstOrDefault(s => s.Id.Equals(siteId));
            if (!data.Id.Equals(siteId)) return false;
            this.Sites.Remove(data);
            data.Name = siteName;
            this.Sites.Add(data);
            return true;
        }

        public IEnumerable<string> GetSiteLanguagesBySiteId(int siteId) {
            if (this.SiteLanguages.ContainsKey(siteId)) {
                var returnEnumer = new SortedList<int, string>();
                if (this.SiteDefaultLanguages.ContainsKey(siteId)) {
                    returnEnumer.Add(0, this.SiteDefaultLanguages[siteId]);
                }
                foreach (var siteLang in this.SiteLanguages[siteId].OrderBy(k => k.Key)) {
                    returnEnumer.Add(returnEnumer.Count, siteLang.Value);
                }
                return returnEnumer.Values;
            } else
                return new string[0];
        }

        public void SaveSiteDefaultLanguage(int siteId, string culture) {
            this.SiteDefaultLanguages[siteId] = culture;
        }

        public void AddSiteLanguage(int siteId, string culture) {
            if (!this.SiteLanguages.ContainsKey(siteId))
                this.SiteLanguages.Add(siteId, new SortedList<int, string>());
            this.SiteLanguages[siteId].Add(this.SiteLanguages[siteId].Count, culture);
        }

        public void RemoveSiteLanguage(int siteId, string culture) {
            if (!this.SiteLanguages.ContainsKey(siteId)) return;
            var idxValue = this.SiteLanguages[siteId].IndexOfValue(culture);
            if (idxValue >= 0) this.SiteLanguages[siteId].RemoveAt(idxValue);
        }

        #endregion

    }
}
